package com.likai.authority.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.likai.authority.pojo.User;
import com.likai.authority.service.IUserService;
import com.likai.authority.util.AnswerInfo;
import com.likai.authority.util.PageList;
import com.likai.authority.util.StringUtil;

@Controller
public class UserController {
	
	@Autowired
	@Qualifier("userService")
	private IUserService userService ;
	
	@RequestMapping(value="/userList",method=RequestMethod.GET)
	public ResponseEntity<PageList> userList(
			Integer page,
			Integer rows,
			String username ,
			String roleid
			) {
		User po = new User() ;
		if(StringUtil.isNotEmpty(username)) {
			po.setUsername(username);
		} 
		if(StringUtil.isNotEmpty(roleid)) {
			po.setRoleid(roleid);
		}
		
		PageHelper.startPage(page, rows) ;
		List<User> list = userService.findListAndRoleName(po) ;
		PageInfo pageInfo = new PageInfo(list,5) ;
		PageList pageList = new PageList(
				pageInfo.getTotal(),
				pageInfo.getList()
				) ;
		return new ResponseEntity<PageList>(pageList,HttpStatus.OK) ;
	}

	@RequestMapping(value="/updatePassword",method=RequestMethod.POST)
	public ResponseEntity<AnswerInfo> updatePassword(
			String username,
			String password,
			String newPassword,
			HttpServletRequest request
			) {
		User po = new User() ;
		po.setUsername(username);
		po.setPassword(password);
		AnswerInfo json = new AnswerInfo() ;
		User user = userService.login(po) ;
		if(user != null) {
			user.setPassword(newPassword);
			int result = userService.updatePassword(user) ;
			if(result == 1) {
				json.setData("修改成功");
				request.getSession().setAttribute("user", user);
				return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
			} else {
				json.setData("修改失败");
				return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
			}
		} else {
			json.setData("密码错误");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	public ResponseEntity<AnswerInfo> addUser(
			User po
			){
		AnswerInfo json = new AnswerInfo() ;
		int result = userService.updayeUser(po) ;
		if(result == 1) {
			json.setData("添加成功");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("添加失败");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/vaildUserName",method=RequestMethod.GET)
	public ResponseEntity<AnswerInfo> vaildUserName(
			String username
			) {
		AnswerInfo json = new AnswerInfo() ;
		User user = userService.findUserByUsername(username) ;
		if(user == null) {
			json.setData("true");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("false");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/minute/{id}",method=RequestMethod.GET)
	public ResponseEntity<User> minute(
			@PathVariable(value="id") String id
			) {
		User user = userService.findUserByUserid(id) ;
		if(user != null) {
			return new ResponseEntity<User>(user,HttpStatus.OK) ;
		} else {
			return new ResponseEntity<User>(HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/remove/{ids}",method=RequestMethod.DELETE)
	public ResponseEntity<AnswerInfo> remove(
			@PathVariable(value="ids") String [] ids
			) {
		int result = userService.removeUser(ids) ;
		AnswerInfo json = new AnswerInfo()  ;
		if(result == 1) {
			json.setData("删除成功");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("删除失败");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
}
